package API.SystemDome;

import java.util.Arrays;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * ClassName:User
 * Package:API.SystemDome
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/1/31-12:41
 * @Version:v1.0
 */
public class User implements Cloneable{
    //Cloneable
    /*
    * 如果一个接口里面没有抽象方法
    * 表示当前的接口是一个标记性接口
    * 现在Cloneable表示一旦实现了,那么当前类的对象就可以被克隆
    * 如果没有实现,当前类的对象就不能克隆
    * */
        private int id;
        private String username;
        private String password;
        private String path;
        private int[] date;


        public User() {
        }

        public User(int id, String username, String password, String path, int[] date) {
            this.id = id;
            this.username = username;
            this.password = password;
            this.path = path;
            this.date = date;
        }

        /**
         * 获取
         * @return id
         */
        public int getId() {
            return id;
        }

        /**
         * 设置
         * @param id
         */
        public void setId(int id) {
            this.id = id;
        }

        /**
         * 获取
         * @return username
         */
        public String getUsername() {
            return username;
        }

        /**
         * 设置
         * @param username
         */
        public void setUsername(String username) {
            this.username = username;
        }

        /**
         * 获取
         * @return password
         */
        public String getPassword() {
            return password;
        }

        /**
         * 设置
         * @param password
         */
        public void setPassword(String password) {
            this.password = password;
        }

        /**
         * 获取
         * @return path
         */
        public String getPath() {
            return path;
        }

        /**
         * 设置
         * @param path
         */
        public void setPath(String path) {
            this.path = path;
        }

        /**
         * 获取
         * @return date
         */
        public int[] getDate() {
            return date;
        }

        /**
         * 设置
         * @param date
         */
        public void setDate(int[] date) {
            this.date = date;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            User user = (User) o;
            return id == user.id && Objects.equals(username, user.username) && Objects.equals(password, user.password) && Objects.equals(path, user.path) && Arrays.equals(date, user.date);
        }

//        @Override
//        public int hashCode() {
//            int result = Objects.hash(id, username, password, path);
//            result = 31 * result + Algorithm.Arrays.hashCode(date);
//            return result;
//        }

        public String toString() {
            return "角色编号为："+id +" 用户名"+username+" 密码"+password +" 游戏图片" + path +" 进度"+ arrtoString();
        }

        private String arrtoString() {
            StringJoiner sj = new StringJoiner(",","[","]");
            for (int i = 0; i < date.length; i++) {
                sj.add(date[i]+" ");
            }
            return sj.toString();
        }

    @Override
    public Object clone() throws CloneNotSupportedException {
        //重写clone方法
        //调用父类中的clone方法
        //相当于让java帮我们克隆一个对象,并把克隆之后的对象返回出去

        /*
        深克隆

        * 先把被克隆对象中的数组获取出来*/
        int []date = this.date;
        //创建新的数组
        int[]newData = new int[date.length];
        //拷贝数组中的数组
        for (int i = 0; i < date.length; i++) {
            newData[i]=date[i];
        }
        //调用父类中的方法克隆对象
        User u = (User) super.clone();
        //因为父类中的克隆方法是浅克隆,替换克隆出来对象中的数组地址值
        u.date = newData;
        return u;
//        return super.clone();



    }
}
